package com.jama.carouselviewexample.slice;

import com.jama.carouselviewexample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.text.Font;

import java.util.ArrayList;
import java.util.List;

/**
 * ImageCarouselActivitySlice
 *
 * @since 2021-05-20
 */
public class ImageCarouselSlice extends AbilitySlice implements Component.ClickedListener {
    private List<String> imageTitle;
    private List<String> imageDesc;
    private List<StackLayout> textImageViews;
    private PageSlider pageSlider;
    private Image btLeft;
    private Image btRight;
    private Text tvNowNum;
    private Text tvAllNum;
    private Text tvImageMessage;
    private int nowPageNum;

    /**
     * onStart
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_image_carousel_activity);
        initView();
    }

    private void initView() {
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider_image);
        btLeft = (Image) findComponentById(ResourceTable.Id_bt_left);
        btRight = (Image) findComponentById(ResourceTable.Id_bt_right);
        tvAllNum = (Text) findComponentById(ResourceTable.Id_tv_allNum);
        tvNowNum = (Text) findComponentById(ResourceTable.Id_tv_nowNum);
        tvImageMessage = (Text) findComponentById(ResourceTable.Id_tv_image_message);
        btRight.setClickedListener(this);
        btLeft.setClickedListener(this);
        imageTitle = createTitleList();
        imageDesc = createDesc();
        textImageViews = createTextImage();
        pageSlider.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return textImageViews.size();
            }
            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                StackLayout textImageView = textImageViews.get(i);componentContainer.addComponent(textImageView);
                return textImageView;
            }
            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) { componentContainer.removeComponent((Component) o); }
            @Override
            public boolean isPageMatchToObject(Component component, Object obj) {
                return component == obj;
            }
        });
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int index) {
                nowPageNum = index;
                tvImageMessage.setText(imageDesc.get(index));
                tvNowNum.setText((index + 1) + "");
                tvNowNum.invalidate();
            }
        });


    }

    private List<String> createTitleList() {
        List<String> strings = new ArrayList<>();
        strings.add("Cape Town、 South Africa");
        strings.add("New York、 USA");
        strings.add("Iceland");
        return strings;
    }

    private List<String> createDesc() {
        List<String> strings = new ArrayList<>();
        strings.add("Broad walk by the ocean");
        strings.add("Journal and coffee at table");
        strings.add("Tying down tent fly");
        return strings;
    }

    private List<StackLayout> createTextImage() {
        List<StackLayout> layouts = new ArrayList<>();
        layouts.add(createtextimageview(ResourceTable.Media_boardwalk_by_the_ocean, imageTitle.get(0)));
        layouts.add(createtextimageview(ResourceTable.Media_journal_and_coffee_at_table, imageTitle.get(1)));
        layouts.add(createtextimageview(ResourceTable.Media_tying_down_tent_fly, imageTitle.get(2)));

        return layouts;
    }

    private StackLayout createtextimageview(int imageid, String imageMessage) {
        Text textview;
        Image imageView;

        StackLayout stackLayout = (StackLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_text_image_layout, null, false);
        textview = (Text) stackLayout.findComponentById(ResourceTable.Id_tv_text_image);
        imageView = (Image) stackLayout.findComponentById(ResourceTable.Id_iv_text_image);
        textview.setFont(Font.DEFAULT_BOLD);
        textview.setText(imageMessage);
        imageView.setPixelMap(imageid);
        return stackLayout;
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * onClick
     *
     * @param component component
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_bt_left:
                int index = nowPageNum - 1;
                if (index >= 0) {
                    pageSlider.setCurrentPage(index);
                } else {
                    pageSlider.setCurrentPage(0);
                }
                break;
            case ResourceTable.Id_bt_right:
                int i1 = nowPageNum + 1;
                if (i1 <= 2) {
                    pageSlider.setCurrentPage(i1);
                } else {
                    pageSlider.setCurrentPage(2);
                }
                break;
            default:
                break;
        }
    }
}
